home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xfcn / newfilen.sit / NewFileName XFCN / card_2549.txt < prev    next >
Encoding:
Text File  |  1987-12-01  |  11.2 KB  |  351 lines

  1. -- card: 2549 from stack: in
  2. -- bmap block id: 3801
  3. -- flags: 0000
  4. -- background id: 2716
  5. -- name: 
  6.  
  7.  
  8. -- part 1 (field)
  9. -- low flags: 01
  10. -- high flags: 0007
  11. -- rect: left=3 top=23 right=337 bottom=438
  12. -- title width / last selected line: 0
  13. -- icon id / first selected line: 0 / 0
  14. -- text alignment: 0
  15. -- font id: 22
  16. -- text size: 9
  17. -- style flags: 0
  18. -- line height: 12
  19. -- part name: 
  20.  
  21.  
  22. -- part 2 (button)
  23. -- low flags: 00
  24. -- high flags: 0000
  25. -- rect: left=452 top=37 right=90 bottom=503
  26. -- title width / last selected line: 0
  27. -- icon id / first selected line: 0 / 0
  28. -- text alignment: 1
  29. -- font id: 0
  30. -- text size: 12
  31. -- style flags: 0
  32. -- line height: 16
  33. -- part name: 
  34. ----- HyperTalk script -----
  35. on mouseUp
  36.   get NewFileName("Name for source file:","NewFileName.p")
  37.   if it is not empty then
  38.     put it into theFile
  39.     open file theFile
  40.     write card field 1 to file theFile
  41.     close file theFile
  42.   end if
  43. end mouseUp
  44.  
  45.  
  46.  
  47. -- part 5 (button)
  48. -- low flags: 00
  49. -- high flags: 2000
  50. -- rect: left=466 top=286 right=313 bottom=495
  51. -- title width / last selected line: 0
  52. -- icon id / first selected line: 26425 / 26425
  53. -- text alignment: 1
  54. -- font id: 3
  55. -- text size: 9
  56. -- style flags: 0
  57. -- line height: 12
  58. -- part name: Look at this script
  59. ----- HyperTalk script -----
  60. on mouseUp
  61.   visual iris open to black
  62.   visual dissolve fast
  63.   go to next card
  64. end mouseUp
  65.  
  66.  
  67.  
  68. -- part contents for card part 1
  69. ----- text -----
  70. {
  71.           NewFileName
  72.             ***********
  73.             A HyperCard "XFCN" (External Function) Resource
  74.             Version 1.0
  75.  
  76. Written By: Steve Maller
  77.              Apple Computer Training Support
  78.              Copyright ┬⌐ 1987 Apple Computer
  79.              AppleLink: MALLER1
  80.             Monday, November 30, 1987
  81.  
  82. Language:    MPW Pascal
  83.  
  84. To build:  pascal NewFileName.p
  85.            link -m ENTRYPOINT -rt XFCN=914 -sn Main=NewFileName Γêé
  86.               -t STAK -c WILD Γêé
  87.               NewFileName.p.o Γêé
  88.               hd:dev:mpw:libraries:Interface.o Γêé
  89.               hd:dev:mpw:PLibraries:Paslib.o Γêé
  90.               -o "NewFileName XFCN"
  91.  
  92. Usage:      NewFileName("prompt", "defaultName")
  93.                                     -- "prompt" & "defaultName" are optional
  94.  
  95. Examples:    NewFileName("Enter a name:", "My file")
  96.                     -- prompts the user with a default name of "My file"
  97.             NewFileName("Enter a name:")
  98.                     -- prompts the user with no default name
  99.             
  100. Result:      The full pathname of the file the user chose to create.
  101.             THIS XFCN DOES NOT CREATE THE FILE; HYPERCARD MUST!
  102.             For example, if you selected the file "Address Stack" which is
  103.             in the folder "My Stacks" in the folder "HyperCard" on the
  104.             disk "HD" the result is:
  105.                   HD:HyperCard:My Stacks:Address Stack
  106.             
  107. Warning:    A word of caution: the MacΓÇÖs file system can NOT accept
  108.             pathnames longer than 255 characters. Be careful...
  109.             
  110. Script
  111. Example:    on mouseUp
  112.               put NewFileName("Enter a new name:") into theFile
  113.               if theFile is not empty then
  114.                 open file theFile
  115.                 write container to file theFile
  116.                 close file theFile
  117.               end if
  118.             end mouseUp
  119.  
  120. Why?        You must access files in HyperCard by their full pathname.
  121.             Unfortunately, HyperCard offers you no clear way of finding
  122.             out what that full name is. If files are on a hard disk, it
  123.             can be a real pain to remember the entire pathname. This
  124.             function simplifies that task for both the stackware developer
  125.             and the end user.
  126.  
  127. Thanks to:  The HyperCard Team - my heros!
  128.  
  129. }
  130.  
  131. {$S NewFileName }
  132.  
  133. UNIT Snoopy_Vs_TheRedBaron; { obviously this name is irrelevant }
  134.  
  135. { =----------------------INTERFACE----------------------= }
  136.  
  137.   INTERFACE
  138.  
  139.     USES
  140.       {$LOAD PasSymDump}
  141.       MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf, HyperXCmd;
  142.  
  143.     PROCEDURE EntryPoint(paramPtr: XCmdPtr);
  144.  
  145. { =----------------------IMPLEMENTATION----------------------= }
  146.  
  147.   IMPLEMENTATION
  148.  
  149.     {$R-}          
  150.     { no Pascal range checking }
  151.  
  152.     TYPE
  153.       Str31 = String[31];          { for the glue file ΓÇ£XCmdGlue.incΓÇ¥ }
  154.  
  155.     PROCEDURE NewFileName(paramPtr: XCmdPtr);
  156.       FORWARD;
  157.  
  158.         { =----------------------EntryPoint----------------------= }
  159.  
  160.     PROCEDURE EntryPoint(paramPtr: XCmdPtr);
  161.  
  162.       BEGIN
  163.         NewFileName(paramPtr);
  164.       END;
  165.  
  166.         { =----------------------NewFileName----------------------= }
  167.  
  168.     PROCEDURE NewFileName(paramPtr: XCmdPtr);
  169.  
  170.       VAR
  171.         myWDPB: WDPBPtr; { some variants of the same animal }
  172.         myCPB: CInfoPBPtr;
  173.         myPB: HParmBlkPtr;
  174.         fullPathName: Str255;
  175.         userPrompt: Str255;  { the "prompt" parameter }
  176.         defaultName: Str255;  { the "defaultName" parameter }
  177.         numTypes: Integer;
  178.         reply: SFReply;
  179.         typeList: SFTypeList;
  180.  
  181.         {$I XCmdGlue.inc }
  182.  
  183.         { =----------------------CenterWindow----------------------= }
  184.  
  185.       PROCEDURE CenterWindow(w: WindowPtr);
  186.       
  187.       { a general-purpose routine that will center the window whoΓÇÖs 
  188.         WindowPtr is passed in w. Here it is used to center the still-
  189.         invisible SFGetFile dialog box. WeΓÇÖll just steal the coordinates
  190.         of the upper-left corner of the dialog to pass to SFGetFile... }
  191.  
  192.         VAR
  193.           hWindSize: Integer;
  194.           vWindSize: Integer;
  195.           hSize: Integer;
  196.           vSize: Integer;
  197.  
  198.         BEGIN
  199.           WITH w^.portBits.bounds DO    { find out how big the SCREEN is }
  200.             BEGIN
  201.               hSize := right - left;    { NOTE: this centers the window }
  202.               vSize := bottom - top;    { within screenBits.bounds ONLY. }
  203.             END;                        { It will NOT recognize multiple }
  204.                                         { monitors on a Mac II... } 
  205.           WITH w^.portRect DO
  206.             BEGIN
  207.               hWindSize := right - left;  { get the size of the window }
  208.               vWindSize := bottom - top;
  209.             END;
  210.             
  211.           { now move the window to the appropriate place on the screen }
  212.  
  213.           MoveWindow(w, ((hSize - hWindSize) DIV 2),
  214.                         ((vSize - vWindSize + 20) DIV 2), FALSE);
  215.         END;
  216.  
  217.       { =----------------------TheyChoseAFile----------------------= }
  218.  
  219.       FUNCTION TheyChoseAFile: Boolean;
  220.  
  221.         VAR
  222.           pt: Point;
  223.           wPtr: WindowPtr;
  224.           savePort: WindowPtr;
  225.  
  226.         BEGIN
  227.           TheyChoseAFile := FALSE;
  228.  
  229.           GetPort(savePort);              { save the current grafport }
  230.           
  231.           { load in the SFGetFile DLOG resource for perousal }
  232.           
  233.           wPtr := GetNewDialog(putDlgID, NIL, POINTER( - 1));
  234.  
  235.           SetPort(wPtr);                  { set port to it for LocalToGlobal }
  236.           CenterWindow(wPtr);              { center (still invisible) window }
  237.           pt := wPtr^.portRect.topLeft;    { is 0,0 - but no assumptions! }
  238.           LocalToGlobal(pt);              { convert this into global coords }
  239.           
  240.           SFPutFile(pt, userPrompt, defaultName, NIL, reply);
  241.           
  242.           SetPort(savePort);              { restore the grafport }
  243.  
  244.           IF reply.good THEN { if they didnΓÇÖt choose Cancel }
  245.             BEGIN
  246.               TheyChoseAFile := TRUE;
  247.               fullPathName := reply.fName; { start the ball rolling }
  248.             END;
  249.         END;
  250.  
  251.       { =----------------------BuildThePathName----------------------= }
  252.  
  253.       PROCEDURE BuildThePathName;
  254.  
  255.         VAR
  256.           name: Str255;
  257.           err: Integer;
  258.  
  259.         BEGIN
  260.           name := '';                           { start with an empty name }
  261.           myPB^.ioNamePtr := @name;             { we want the Volume name }
  262.           myPB^.ioCompletion := POINTER(0);
  263.           myPB^.ioVRefNum := reply.vRefNum;     { returned from SFGetFile }
  264.           myPB^.ioVolIndex := 0;                 { use the vRefNum and name }
  265.           err := PBHGetVInfo(myPB, FALSE);       { fill in the Volume info }
  266.           IF err <> noErr THEN
  267.             Exit(NewFileName);
  268.  
  269. { Now we need the Working Directory (WD) information because weΓÇÖre going
  270.  to step backwards from the file through all of the the folders until
  271.  we reach the root directory }
  272.  
  273.           myWDPB^.ioVRefNum := reply.vRefNum;   { this got set to 0 above }
  274.           myWDPB^.ioWDProcID := 0;               { use the vRefNum }
  275.           myWDPB^.ioWDIndex := 0;               { we want ALL directories }
  276.           err := PBGetWDInfo(myWDPB, FALSE);     { do it }
  277.           IF err <> noErr THEN
  278.             Exit(NewFileName);
  279.  
  280.           myCPB^.ioFDirIndex := - 1;             { use the ioDirID field only }
  281.           myCPB^.ioDrDirID := myWDPB^.ioWDDirID; { info returned above }
  282.           err := PBGetCatInfo(myCPB, FALSE);     { do it }
  283.           IF err <> noErr THEN
  284.             Exit(NewFileName);
  285.  
  286. { Here starts the real work - start to climb the tree by continually
  287.  looking in the ioDrParId field for the next directory above until we
  288.  fail... }
  289.  
  290.           myCPB^.ioDrDirID := myCPB^.ioDrParId;     { the first folder}
  291.           fullPathName := Concat(myCPB^.ioNamePtr^, ':', reply.fName);
  292.           REPEAT
  293.             myCPB^.ioDrDirID := myCPB^.ioDrParId;
  294.             err := PBGetCatInfo(myCPB, FALSE);       { the next level }
  295.  
  296. { Be careful of an error returned here - it means the user chose a file
  297.  on the desktop level of this volume. If this is the case, just stop
  298.  here and return "VolumeName:NewFileName", otherwise loop until failure }
  299.             IF err = noErr THEN
  300.               fullPathName := Concat(myCPB^.ioNamePtr^, ':', fullPathName);
  301.  
  302.           UNTIL err <> noErr;
  303.  
  304.         END; { PROCEDURE BuildThePathName }
  305.  
  306.       { =---------------------- * NewFileName * ----------------------= }
  307.  
  308.       BEGIN { PROCEDURE NewFileName }
  309.  
  310. { First we allocate some memory in the heap for the parameter block. This
  311.  could in theory work on the stack, but in reality it makes no difference
  312.  as weΓÇÖre entirely modal (ugh) here... }
  313.  
  314.         fullPathName := '';              { -EMPTY- if we fail! }
  315.         userPrompt := '';                { the "prompt" parameter }
  316.         defaultName := '';              { the "defaultName" parameter }
  317.  
  318.         myCPB := CInfoPBPtr(NewPtr(SizeOf(HParamBlockRec)));
  319.         IF ord4(myCPB) <= 0 THEN
  320.           Exit(NewFileName);             { Rats! Bill didnΓÇÖt leave enough room }
  321.         myWDPB := WDPBPtr(myCPB);       { icky Pascal type coercions... }
  322.         myPB := HParmBlkPtr(myCPB);
  323.  
  324.         WITH paramPtr^ DO
  325.           BEGIN
  326.             IF paramCount >= 1 THEN
  327.               ZeroToPas(params[1]^, userPrompt);
  328.             IF paramCount = 2 THEN
  329.               ZeroToPas(params[2]^, defaultName);
  330.  
  331.             IF TheyChoseAFile THEN
  332.               BuildThePathName;
  333.  
  334. { PasToZero is very interesting - it is a HyperTalk command
  335.  that you can actually call from OUTSIDE of HyperCard.
  336.  You need it because HyperCard uses C format strings with
  337.  no length byte; they are terminated by a null byte. They are
  338.  actually HANDLES to C format strings. Nice work, Dan! }
  339.  
  340.             returnValue := PasToZero(fullPathName);
  341.  
  342.           END; { WITH paramPtr^ DO }
  343.  
  344.         DisposPtr(POINTER(myCPB));       { Thou Shalt Clean Up Thy Heap! }
  345.         
  346.         numTypes := StringWidth('NewFileName version 1.0 ΓÇó ┬⌐1987 Steve Maller');
  347.  
  348.       END; { PROCEDURE NewFileName }
  349.  
  350. END.
  351.